<template>
  <Card title="工具条按钮" :bordered="false" :head-style="headStyle" :loading="loading">
    <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'leftBottom',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <ButtonListDrawer @register="registerDrawer" @success="handleSuccess" />
  </Card>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { Card } from 'ant-design-vue';
  import { useDrawer } from '/@/components/Drawer';
  import ButtonListDrawer from './ButtonListDrawer.vue';
  import { columns } from './buttonList.data';
  import { getVisualListToolbarCustomBtns, getVisualBtnDel } from '/@/api/module/module';

  export default defineComponent({
    name: 'ButtonList',
    components: { BasicTable, ButtonListDrawer, TableAction, Card },
    props: {
      formCode: {
        type: String,
        default: '',
      },
      moduleCode: {
        type: String,
        default: '',
      },
      headStyle: {
        type: Object,
        default: () => {
          return {};
        },
      },

      formGetFieldsData: {
        type: Array,
        default: () => [],
      },
    },
    setup(props) {
      const loading = ref(false);

      const [registerDrawer, { openDrawer }] = useDrawer();
      onMounted(async () => {});

      const [registerTable, { reload, getDataSource }] = useTable({
        title: '',
        api: getVisualListToolbarCustomBtns,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: [],
        },
        searchInfo: {
          // op: 'search',
        }, //额外的参数
        useSearchForm: false,
        showTableSetting: false,
        bordered: true,
        indexColumnProps: { width: 50 },
        pagination: false,
        showIndexColumn: true,
        canResize: false,
        beforeFetch: (info) => {
          let newInfo = info;
          newInfo.moduleCode = props.moduleCode;
          return newInfo;
        },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      //普通新增
      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
          formCode: props.formCode,
          moduleCode: props.moduleCode,
          dataSourceLength: getDataSource().length,
          formGetFieldsData: props.formGetFieldsData,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          formCode: props.formCode,
          moduleCode: props.moduleCode,
          isUpdate: true,
          formGetFieldsData: props.formGetFieldsData,
        });
      }

      async function handleDelete(record: Recordable) {
        loading.value = true;
        const params = {
          btnName: record.name,
          formCode: props.formCode,
          code: props.moduleCode,
        };
        await getVisualBtnDel(params).then(() => {
          loading.value = false;
          handleSuccess();
        });
      }

      function handleSuccess() {
        reload();
      }
      onMounted(() => {});
      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
        loading,
      };
    },
  });
</script>
